package com.a01_2021.thousandstimes.general.controller;


import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.general.entity.PersonnelInfomation;
import com.a01_2021.thousandstimes.general.jwt.JwtTokenUtil;
import com.a01_2021.thousandstimes.general.service.PersonnelInfomationService;
import com.a01_2021.thousandstimes.saas.entity.vo.InforQuery;
import com.a01_2021.thousandstimes.utils.AjaxResponse;
import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author FRQ
 * @since 2021-02-22
 */
@RestController
@RequestMapping("/personnel-infomation")
public class PersonnelInfomationController {

    @Autowired
    private PersonnelInfomationService personnelInfomationService;

    @Resource
    private JwtTokenUtil jwtTokenUtil;

    @PreAuthorize("hasAuthority('personnel-infomation-addInfo')")
    @DS("#header.instanceid")
    @PostMapping("addInfo")
    public AjaxResponse register(@RequestBody(required = false) InforQuery inforQuery) {
        try {
            return personnelInfomationService.insertInfo(inforQuery);
        } catch (Exception e) {
            return AjaxResponse.error("个人信息填写失败，请重新填写");
        }

    }

    /**
     * 上传简历
     *
     * @param instanceId 实例id
     * @param token      jwt令牌
     * @param file       上传文件
     * @return AjaxResponse
     * @throws IOException     IO异常
     * @throws CustomException 当文件名不符合后缀时抛出该异常
     */
    @PreAuthorize("hasAuthority('personnel-infomation-uploadResume')")
    @DS("#header.instanceid")
    @PostMapping("uploadResume")
    public AjaxResponse uploadResume(@RequestHeader("instanceid") String instanceId, @RequestHeader("${jwt.header}") String token, MultipartFile file) throws IOException, CustomException {
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        HashMap<String, String> resultMap = personnelInfomationService.uploadResume(instanceId, userId, file);
        return AjaxResponse.success(resultMap);
    }

    /**
     * 修改个人信息
     *
     * @param token               jwt令牌
     * @param personnelInfomation 个人信息实体类
     * @return AjaxResponse
     * @throws CustomException 当个人信息条目不存在,修改他人个人信息时抛出该异常
     */
    @PreAuthorize("hasAuthority('personnel-infomation-update')")
    @DS("#header.instanceid")
    @PostMapping("update")
    public AjaxResponse updateInfo(@RequestHeader("${jwt.header}") String token, PersonnelInfomation personnelInfomation) throws CustomException {
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        PersonnelInfomation resultItem = personnelInfomationService.updateInfo(userId, personnelInfomation);
        return AjaxResponse.success(resultItem);
    }

    /**
     * 用于获取个人信息
     *
     * @param token jwt令牌
     * @return AjaxResponse
     */
    @PreAuthorize("hasAuthority('personnel-infotmation-get')")
    @DS("#header.instanceid")
    @PostMapping("get")
    public AjaxResponse get(@RequestHeader("${jwt.header}") String token) {
        long userId = Long.parseLong(jwtTokenUtil.getUserIdFromToken(token));
        PersonnelInfomation resultItem = personnelInfomationService.getByUserId(userId);
        return AjaxResponse.success(resultItem);
    }
}
